package com.gt.utils;

import org.apache.commons.lang3.RandomStringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

//随机码生成
public class RandomCodeUtil {
    //用户Id生成
    public static String buildUserId(){
        Random rand = new Random();
        char[] letters1=new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',
                'R','S','T','U','V','W','X','Y','Z'};
        char[] letters2=new char[]{'0','1','2','3','4','5','6','7','8','9'};
        String str = letters1[rand.nextInt(letters2.length)]+"";
        int index;
        boolean[] flags = new boolean[letters2.length];//默认为false
        for(int i=0;i<6;i++){
            do{
                index = rand.nextInt(letters2.length);
            }while(flags[index]==true);
            char c = letters2[index];
            str += c;
            flags[index]=true;
        }
       return str;
    }

    public static String buildUserId2(){
        return RandomStringUtils.random(15, true, true);
    }

    //验证码生成
    public static String buildCode(int count){
        Random rand = new Random();
        char[] letters2=new char[]{'0','1','2','3','4','5','6','7','8','9'};
        String str = "";
        int index;
        boolean[] flags = new boolean[letters2.length];//默认为false
        for(int i=0;i<count;i++){
            do{
                index = rand.nextInt(letters2.length);
            }while(flags[index]==true);
            char c = letters2[index];
            str += c;
            flags[index]=true;
        }
        return str;
    }

    //生成订单号
    public static String buildOrderNo(){
        return (new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())) + buildCode(3);
    }

    //生成随机英文字母
    public static String buildLetters(int count){

        Random rand = new Random();
        char[] letters=new char[]{
//                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q', 'R','S','T','U','V','W','X','Y','Z',
                'a','b','c','d','e','f','g','h','i', 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        String str = "";
        int index;
        boolean[] flags = new boolean[letters.length];//默认为false
        for(int i=0;i<count;i++){
            do{
                index = rand.nextInt(letters.length);
            }while(flags[index]==true);
            char c = letters[index];
            str += c;
            flags[index]=true;
        }
        return str;

    }

}
